package br.ufc.cloud;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.images.ImagesService;
import com.google.appengine.api.images.ImagesServiceFactory;

public class ListarContatos extends HttpServlet {
	

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.getWriter().print("<html>");
		resp.getWriter().print("<body>");
		
		List<Entity> entities = search();

		for (Entity entity : entities) {
			String name, address, email, phone;
			BlobKey photoBlobKey;
			Long idUser = (Long) entity.getProperty("id");
			name = (String) entity.getProperty("name");
			address = (String) entity.getProperty("address");
			email = (String) entity.getProperty("email");
			phone = (String) entity.getProperty("phone");
			photoBlobKey = (BlobKey) entity.getProperty("photoBlobKey");
			WriteHtmlList(name, address, email, phone, photoBlobKey, resp, idUser.longValue());
		}
		
		/*
		resp.getWriter().print("<div style=\"float:left;\">");
		
		resp.getWriter()
		.print("<input  type=\"button\" value=\"Voltar\" onclick=\"history.go(-1)\"");
		
		resp.getWriter().print("</div>");
		*/
		resp.getWriter().print("</body>");
		
		resp.getWriter().print("</html>");
		

	}

	public List<Entity> search() {
		DatastoreService datastore = DatastoreServiceFactory
				.getDatastoreService();
		Query query = new Query("User");
		List<Entity> customers = datastore.prepare(query).asList(
				FetchOptions.Builder.withDefaults());
		return customers;
	}

	public static String getImageUrlFromBlobKey(BlobKey blobKey) {
		ImagesService imagesService = ImagesServiceFactory.getImagesService();
		String imageUrl = imagesService.getServingUrl(blobKey);
		return imageUrl;
	}

	public void WriteHtmlList(String name, String address, String email,
			String phone, BlobKey photoBlobKey, HttpServletResponse resp, long idUser) throws IOException {

			resp.getWriter().print("<div style=\"float:left;\">");
			
		resp.getWriter().print("<table border=\"3\" >");
		
		resp.getWriter().print("<tr><td align=\"center\">");
		resp.getWriter().print("<a href=\"EditarUser?idUser=" + idUser + "\">Editar</a>");
		resp.getWriter().print(" | ");
		resp.getWriter().print("<a href=\"\">Deletar </a>");
		resp.getWriter().print("</td></tr>");
		
		resp.getWriter().print("<tr>");
		resp.getWriter().print("<td>");
		resp.getWriter().print("Nome: " + name);
		resp.getWriter().print("</td>");

		resp.getWriter().print("<tr>");
		resp.getWriter().print("<td>");
		resp.getWriter().print("Endereço: " + address);
		resp.getWriter().print("</td>");

		resp.getWriter().print("<tr>");
		resp.getWriter().print("<td>");
		resp.getWriter().print("E-mail: " + email);
		resp.getWriter().print("</td>");
		resp.getWriter().print("</tr>");

		resp.getWriter().print("<tr>");
		resp.getWriter().print("<td>");
		resp.getWriter().print("Telefone: " + phone);
		resp.getWriter().print("</td>");
		resp.getWriter().print("</tr>");

		if (photoBlobKey != null) {
			resp.getWriter().print("<tr>");
			resp.getWriter().print("<td>");
			resp.getWriter().print(
					"<img width=\"100\" height=\"100\"  src=\""
							+ getImageUrlFromBlobKey(photoBlobKey) + "\"/>");
			resp.getWriter().print("</td>");
		}
		
		resp.getWriter().print("</table>");
		resp.getWriter().print("</div>");
		
	}

}
